package com.zty.xiaomi.server.Controller;

import com.zty.xiaomi.server.Dto.AddServiceOrderDto;
import com.zty.xiaomi.server.Dto.GetServiceInfoDto;
import com.zty.xiaomi.server.Dto.GetServiceOrderInfoDto;
import com.zty.xiaomi.server.Entity.Service.Service;
import com.zty.xiaomi.server.Entity.Service.ServiceOrder;
import com.zty.xiaomi.server.Service.Good.GoodServiceImp;
import com.zty.xiaomi.server.Service.Order.OrderServiceImp;
import com.zty.xiaomi.server.Service.Service.ServiceService;
import com.zty.xiaomi.server.Service.ServiceOrder.ServiceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("serviceOrder")
public class ServiceOrderController {

    @Autowired
    private ServiceOrderService serviceOrderService;

    @Autowired
    private ServiceService serviceService;

    @Autowired
    private OrderServiceImp orderServiceImp;

    @Autowired
    private GoodServiceImp goodServiceImp;

    @PostMapping("addServiceOrder")
    private AddServiceOrderDto addServiceOrder(@RequestBody ServiceOrder serviceOrder) throws IOException {
        AddServiceOrderDto addServiceOrderDto = new AddServiceOrderDto();
        serviceOrder.setStore_id(goodServiceImp.getStoreIdByGoodId(serviceOrder.getGood_id()));
        int num = serviceOrderService.addServiceOrder(serviceOrder);
        if (num == 1) {
            addServiceOrderDto.setId(serviceOrder.getId());
            num = orderServiceImp.updateOrderGoodStatusByOrderNoAndGoodId(serviceOrder.getOrderNo()
                    ,serviceOrder.getGood_id(),1);
            if (num == 1) {
                addServiceOrderDto.setMsg("提交成功！");
                addServiceOrderDto.setStatus("200");
            } else {
                addServiceOrderDto.setStatus("400");
                addServiceOrderDto.setMsg("售后申请成功！订单商品状态修改失败！");
            }
        } else {
            addServiceOrderDto.setMsg("提交失败！");
            addServiceOrderDto.setStatus("400");
        }
        return addServiceOrderDto;
    }

    @GetMapping("getServiceOrderDetailByOrderNoAndGoodId")
    private GetServiceOrderInfoDto getServiceOrderDetailByOrderNoAndGoodId(@RequestParam("orderNo") int orderNo
            , @RequestParam("good_id") int good_id) throws IOException {
        GetServiceOrderInfoDto getServiceOrderInfoDto = new GetServiceOrderInfoDto();
        ServiceOrder serviceOrder = serviceOrderService.getServiceOrderDetailByOrderNoAndGoodId(orderNo, good_id);
        if (serviceOrder == null) {
            getServiceOrderInfoDto.setStatus("400");
            getServiceOrderInfoDto.setMsg("该订单未出现售后情况");
        } else {
            getServiceOrderInfoDto.setStatus("200");
            Service service = serviceService.getServiceByServiceId(serviceOrder.getService_id());
            serviceOrder.setService(service);
            getServiceOrderInfoDto.setData(serviceOrder);
        }
        return getServiceOrderInfoDto;
    }
}
